//
//  FriendViewController.m
//  SeaChat
//
//  Created by 潘强 on 16/6/13.
//  Copyright © 2016年 潘强. All rights reserved.
//

#import "FriendViewController.h"
#import "FriendTableViewCell.h"
#import "ScanViewController.h"
#import "twodimensionVC.h"
//#import "AMapViewController.h"
#import "RuningViewController.h"

static NSString *friendMark = @"friendMark";

@interface FriendViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *friendVC;

@property(nonatomic,retain)UIImageView *getImage;
@end

@implementation FriendViewController

-(void)dealloc{
    [_friendVC release];
    [_getImage release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationItem.title  = @"朋友圈";
    
    self.friendVC = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.friendVC.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.friendVC];
    self.friendVC.dataSource = self;
    self.friendVC.delegate = self;
    
    [self.friendVC registerClass:[FriendTableViewCell class] forCellReuseIdentifier:friendMark];
    
    [_friendVC release];
    
    // Do any additional setup after loading the view.
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:friendMark];
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.FriendLabel.text = @"计步器";
        cell.FriendimageView.image = [UIImage imageNamed:@"计步器"];
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        cell.FriendLabel.text = @"地图";
        cell.FriendimageView.image = [UIImage imageNamed:@"地图"];

    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        cell.FriendLabel.text = @"扫一扫";
        cell.FriendimageView.image = [UIImage imageNamed:@"扫一扫"];

    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        cell.FriendLabel.text = @"二维码";
        cell.FriendimageView.image = [UIImage imageNamed:@"二维码"];

    }

    
    
//设置cell后面那个图片
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 50;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{


    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }

    return 20;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //点击跳转后 cell的选中效果消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == 0 && indexPath.row == 0) {
        NSLog(@"我是计步器点击方法");
        RuningViewController *runingVC = [RuningViewController new];
        

        [self presentViewController:runingVC animated:YES completion:^{
            
            
        }];
        [runingVC release];

           }
    if (indexPath.section == 0 && indexPath.row == 1) {
        NSLog(@"我是地图点击方法");
//        AMapViewController *amapVC = [[AMapViewController alloc] init];
//        [self presentViewController:amapVC animated:YES completion:^{
//            
//            
//        }];
//        [amapVC release];


    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSLog(@"我是扫一扫点击方法");
        ScanViewController *scanVC = [ScanViewController new];
        
        [self.navigationController pushViewController:scanVC animated:YES];
        [scanVC release];
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        NSLog(@"我是二维码点击方法");
       twodimensionVC *twoVC = [twodimensionVC new];
        //twoVC.TwoImageview.image = _getImage.image;
       [self.navigationController pushViewController:twoVC animated:YES];
        [twoVC release];
    }



}



-(void)two{
    
//    twodimensionVC *twoVC = [twodimensionVC new];

    
    // 1. 创建一个二维码滤镜实例(CIFilter)
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 滤镜恢复默认设置
    [filter setDefaults];
    
    // 2. 给滤镜添加数据
    NSString *string = @"http://www.jianshu.com/users/db6844a150fb/latest_articles";
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 3. 生成二维码
    CIImage *image = [filter outputImage];
    // 4. 显示二维码
    //    twoVC.TwoImageview.image = [UIImage imageWithCIImage:image];
    NSLog(@"%@....",[UIImage imageWithCIImage:image]);
    self.getImage.image = [self createNonInterpolatedUIImageFormCIImage:image withSize:500];
    
//    [self.navigationController pushViewController:twoVC animated:YES];

}

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    NSLog(@"222222");
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
    
    
    
}

@end
